<template>
	<view class="shopstore-service" style="padding-top:1rpx" :style="{background:pageColor}">
		<fixed>
			<search @input="toSearch" type="input" :padding="30" :radius="0" backgroundColor="#fff"
				placeholder="请输入门店名称">
			</search>
		</fixed>

		<view @tap.stop="handleStore(index)" v-for="(item,index) in list.data" :key="index">
			<longbingbing-shopstore-list-item :info="item"></longbingbing-shopstore-list-item>
		</view>


		<load-more :noMore="list.current_page >= list.last_page && list.data.length > 0" :loading="loading">
		</load-more>

		<block v-if="!loading&&list.data.length<=0&&list.current_page==1">
			<abnor v-if="location.lat&&location.lng" :tip="[{text: '暂无门店数据'}]" :isCenter="true">
			</abnor>
			<block v-if="!location.lat&&!location.lng">
				<!-- #ifdef H5 -->
				<abnor type="NOT_LOCATION" title="暂无门店数据" :isCenter="true"></abnor>
				<!-- #endif -->
				<!-- #ifndef H5 -->
				<abnor type="NOT_LOCATION" title="暂无门店数据" @confirm="toOpenLocation" :isCenter="true"
					:button="[{ text: '开启定位' , type: 'confirm' }]" btnSize=""></abnor>
				<!-- #endif -->
			</block>
		</block>
		<view class="space-footer"></view>


		<!-- #ifdef APP-PLUS -->
		<longbingbing-open-location-info ref="open_location_info" :isAdd="false"></longbingbing-open-location-info>
		<longbingbing-app-check-auth type="location" ref="app_check_item"
			@confirm="toConfirmOpenLoca"></longbingbing-app-check-auth>
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<user-privacy ref="user_privacy" :show="false"></user-privacy>
		<!-- #endif -->
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import longbingbingShopstoreListItem from "@/components/longbingbing-shopstore-list-item.vue"
	export default {
		components: {
			longbingbingShopstoreListItem
		},
		data() {
			return {
				options: {},
				loading: true,
				param: {
					page: 1,
				},
				list: {
					data: []
				}
			}
		},
		computed: mapState({}),
		onLoad(options) {
			this.options = options
			this.$util.setNavigationBarColor({
				bg: this.primaryColor
			})
			this.initIndex()
		},
		// #ifdef H5
		destroyed() {
			// #endif
			// #ifndef H5
			onUnload() {
					// #endif 
					this.updateUserItem({
						key: 'appShare',
						val: true
					})
				},
				async onShow() {
						if (!this.location.lat && this.locaRefuse) {
							console.log("==onshow===locaRefuse")
							this.toResetUtilLoca()
						}
					},
					onPullDownRefresh() {
						// #ifndef APP-PLUS
						uni.showNavigationBarLoading()
						// #endif
						this.initRefresh()
						uni.stopPullDownRefresh()
					},
					onReachBottom() {
						if (this.list.current_page >= this.list.last_page || this.loading) return;
						this.param.page = this.param.page + 1;
						this.loading = true;
						this.getList();
					},
					watch: {
						// #ifndef H5
						changeOnAddr(newval, oldval) {
							let {
								noloca,
							} = this.noChangeLoca
							if (newval && noloca) {
								this.initUtilLocaData()
							}
						},
						noChangeLoca(newval, oldval) {
							setTimeout(() => {
								let {
									lat,
									lng,
									unix = 0
								} = this.changeOnAddr
								let {
									noloca
								} = this.noChangeLoca
								let cur_unix = this.$util.DateToUnix(this.$util.formatTime(new Date(),
									'YY-M-D h:m:s'))
								if (noloca && ((!lat && !lng) || !unix || (unix && (cur_unix - unix >= 1)))) {
									this.getUtilLocation()
								}
							}, 800)
						}
						// #endif
					},
					methods: {
						...mapMutations(['updateUserItem', 'updateServiceItem', 'updateTechnicianItem', 'updateMapItem',
							'updateDynamicItem', 'updateShopstoreItem'
						]),
						async initIndex(refresh = false) {
							await this.isNotLoca()
							// #ifdef H5
							if (!refresh) {
								this.$jweixin.hideOptionMenu()
							}
							// #endif
						},
						async initRefresh() {
							this.param.page = 1
							await this.initIndex(true)
						},
						async toOpenLocation() {
							// #ifdef APP-PLUS
							if (plus.os.name == 'Android' && plus.navigator.checkPermission(
									'android.permission.ACCESS_FINE_LOCATION') ===
								'undetermined') {
								this.$refs.app_check_item.open()
							} else {
								this.toConfirmOpenLoca()
							}
							// #endif
							// #ifndef APP-PLUS
							this.toConfirmOpenLoca()
							// #endif
						},
						async toConfirmOpenLoca() {
							// #ifdef MP-WEIXIN
							let privacyCheck = this.$refs.user_privacy.check()
							if (privacyCheck) {
								this.$refs.user_privacy.open()
								return
							}
							// #endif
							this.initRefresh()
						},
						async isNotLoca() {
							let {
								location,
								locaRefuse
							} = this

							let {
								status: coach_status = 0,
								coach_position = 0
							} = this.userCoachStatus


							// #ifdef H5
							if (coach_position && coach_status == 2 && (location.lat && location.address !=
									'暂未获取到位置信息')) {
								this.updateUserItem({
									key: 'isChangeCoachLoca',
									val: true
								})
							}
							// #endif


							// #ifdef APP-PLUS
							if (!locaRefuse && (!location.lat || (location.lat && location.address == '暂未获取到位置信息'))) {
								// #endif
								// #ifndef APP-PLUS
								if (!location.lat || (location.lat && location.address == '暂未获取到位置信息')) {
									// #endif

									// #ifdef MP-WEIXIN
									let privacyCheck = this.$refs.user_privacy.check()
									if (privacyCheck || typeof(privacyCheck) == 'string') {
										if (privacyCheck) {
											this.$refs.user_privacy.open()
										}
										this.loading = false
										this.$util.hideAll()
										return
									}
									// #endif

									// #ifndef H5
									if (coach_position && coach_status == 2) {
										let {
											lat: change_lat = 0,
											lng: change_lng = 0,
											unix = 0
										} = this.changeOnAddr
										let cur_unix = this.$util.DateToUnix(this.$util.formatTime(new Date(),
											'YY-M-D h:m:s'))
										let noloca = change_lat && change_lng && (unix && (cur_unix - unix < 3)) ?
											false : true
										if (!noloca) {
											let loca = Object.assign({}, this.location, {
												lat: change_lat,
												lng: change_lng,
												is_util_loca: 1
											})
											this.updateUserItem({
												key: 'location',
												val: loca
											})
										}
										this.updateUserItem({
											key: 'noChangeLoca',
											val: {
												noloca
											}
										})
										if (this.noChangeLoca.noloca) return
										this.initUtilLocaData()
									} else {
										this.getUtilLocation()
									}
									this.updateMapItem({
										key: 'pageActive',
										val: false
									})
									return
									// #endif

									// #ifdef H5
									await this.$util.getUtilLocation()
									this.updateMapItem({
										key: 'pageActive',
										val: false
									})
									// #endif
								}

								this.initUtilLocaData()
							},
							async getUtilLocation() {
									await this.$util.getUtilLocation()
									this.initUtilLocaData()
								},
								async initUtilLocaData() {
										// #ifndef H5
										this.updateUserItem({
											key: 'noChangeLoca',
											val: {
												noloca: false
											}
										})
										// #endif 
										await this.getList()
										let {
											lat = 0, lng = 0, is_util_loca = 0, isIp = true
										} = this.location
										if (lat && lng && is_util_loca) {
											this.$util.getMapInfo(isIp)
										}
									},
									async toResetUtilLoca() {
											// #ifdef APP-PLUS
											let {
												lat: locaLat = 0,
												isIp = true,
											} = this.location

											let {
												appCheckLocaStatus = null
											} = this
											console.log(appCheckLocaStatus, "=====appCheckLocaStatus")
											if (!locaLat && appCheckLocaStatus == 1) {

												let {
													lng = 0,
														lat = 0
												} = await this.$util.getUtilLocation()
												this.$refs.open_location_info.pShow = false
												this.$util.getPage(-1).$refs.open_location_info.pShow = false
												await this.initRefresh()
												this.$util.back()
												let updateArr = ['updateServiceItem', 'updateTechnicianItem',
													'updateMapItem',
													'updateDynamicItem'
												]
												updateArr.map(item => {
													this[item]({
														key: 'pageActive',
														val: false
													})
												})
											}
											// #endif
										},
										async getList() {
												let {
													list: oldList,
												} = this
												let {
													id: cate_id = 0
												} = this.options
												let {
													lat = 0, lng = 0
												} = this.location
												// #ifdef APP-PLUS
												if (!lat && !lng) {
													this.list = {
														data: [],
														last_page: 1,
														current_page: 1
													}
													this.loading = false
													this.$util.hideAll()
													return
												}
												// #endif
												let param = Object.assign({}, this.param, {
													cate_id,
													lat,
													lng
												})

												let newList = await this.$api.shopstore.storeList(param)
												if (this.param.page == 1) {
													this.list = newList
												} else {
													newList.data = oldList.data.concat(newList.data)
													this.list = newList
												}
												this.loading = false
												this.$util.hideAll()
											},
											handleStore(index) {
												let {
													id = 0,
														title = ''
												} = this.list.data[index]
												try {
													this.$util.getPage(-1).form.store_id = id
													this.$util.getPage(-1).form.store_name = title
													this.$util.goUrl({
														url: 1,
														openType: 'navigateBack'
													})
												} catch (e) {
													// #ifdef H5
													history.back()
													// #endif
												}
											},
											toSearch(val) {
												this.param.title = val
												this.getList(1)
											},
						}
					}
</script>

<style lang="scss">
</style>